package com.wcl.demo.ui.activity;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactListener;
import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.wcl.demo.R;
import com.wcl.demo.presenter.AddFriendsPresenter;
import com.wcl.demo.presenter.presenterinterface.PresenterAddFriendsInterface;
import com.wcl.demo.ui.ViewInterface.ViewAddFriendsInterface;
import com.wcl.demo.view.CustomToast;

import butterknife.Bind;

import java.util.List;

import butterknife.OnClick;

/**
 * 添加好友
 * Created by dllo on 15/11/20.
 */
public class AddFriendActivity extends MVPBaseActivity<ViewAddFriendsInterface, AddFriendsPresenter> implements ViewAddFriendsInterface {

    @Bind(R.id.activity_add_friend_edTv)
    EditText activity_add_friend_edTv;//添加好友的搜索处
    @Bind(R.id.add_friend_text_relativeLayout)
    RelativeLayout add_friend_text_relativeLayout;//点击查找之后出现的用户信息那一行
    @Bind(R.id.activity_add_friend_nameTv)
    TextView activity_add_friend_nameTv;//查找的用户的名字

    private List<String> usernames;

    @OnClick(R.id.activity_add_bac_Iv)//返回按钮
    void activity_add_bac_Iv() {
        finish();
    }

    @OnClick(R.id.add_activity_textBtn)
    void add_activity_textBtn() {//查找按钮
        String addFriends = activity_add_friend_edTv.getText().toString();
        if (addFriends.equals("")) {//判断edit输入的是否为空
            Toast.makeText(AddFriendActivity.this, "请填写用户名", Toast.LENGTH_SHORT).show();//提示空
        } else {
            add_friend_text_relativeLayout.setVisibility(View.VISIBLE);
            activity_add_friend_nameTv.setText(activity_add_friend_edTv.getText().toString());
        }
    }

    @OnClick(R.id.activity_add_friendBtn)
    void activity_add_friendBtn(View v) {

        //获取好友好友列表
        try {
            usernames = EMContactManager.getInstance().getContactUserNames();//需异步执行
        } catch (EaseMobException e) {
            e.printStackTrace();
        }

        if (EMChatManager.getInstance().getCurrentUser().equals(activity_add_friend_nameTv.getText().toString())) {//判断输入的是否是本人
            Toast.makeText(this, "就算你是人格分裂,也不能和自己做朋友", Toast.LENGTH_SHORT).show();
        } else if (usernames.contains(activity_add_friend_nameTv.getText().toString())) {
            //提示已在好友列表中(在黑名单列表里)，无需添加
            if (EMContactManager.getInstance().getBlackListUsernames().contains(activity_add_friend_nameTv.getText().toString())) {
                Toast.makeText(this, "此用户已被拉黑,如需添加请在黑名单中移除", Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(this, "此用户已经是你的好友,无需重复添加", Toast.LENGTH_SHORT).show();
            return;
        } else {

            addContact();
        }


        new Thread(new Runnable() {
            @Override
            public void run() {
                String reason = "加个好友呗";
                try {
                    EMContactManager.getInstance().addContact(activity_add_friend_nameTv.getText().toString(), reason);
                    Log.i("shaco", reason + activity_add_friend_nameTv.getText().toString());
                    setAddFriendInvited();
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
    public void setAddFriendInvited() {
        EMContactManager.getInstance().setContactListener(new EMContactListener() {

            @Override
            public void onContactAgreed(String username) {
                try {
                    EMChatManager.getInstance().acceptInvitation(username);
                    Log.i("lanou" , "好友请求"+username);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onContactRefused(String username) {
                //好友请求被拒绝
                try {
                    EMChatManager.getInstance().refuseInvitation(username);
                    Log.i("lanou" , "拒绝请求"+ username);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onContactInvited(String username, String reason) {
                //收到好友邀请
            }

            @Override
            public void onContactDeleted(List<String> usernameList) {
                //被删除时回调此方法
            }


            @Override
            public void onContactAdded(List<String> usernameList) {
                //增加了联系人时回调此方法
            }
        });

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_addfriend;
    }

    @Override
    protected void initView() {
        Drawable drawable = getResources().getDrawable(R.mipmap.ic_topbar_search);
        drawable.setBounds(10, 0, 60, 50);//第一0是距左边距离，第二0是距上边距离，40分别是长宽
        activity_add_friend_edTv.setCompoundDrawables(drawable, null, null, null);//只放左边
    }

    @Override
    protected void initData() {

    }

    private Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Log.i("lanou","---------------->日了");
            try {
                EMContactManager.getInstance().addContact(activity_add_friend_nameTv.getText().toString(), "baby,交个朋友好么.");
                CustomToast.toastShow(AddFriendActivity.this,"baby,交个朋友好么.");
            } catch (EaseMobException e) {
                e.printStackTrace();
            }

            return false;
        }
    });
    @Override
    protected AddFriendsPresenter createPresenter() {
        return new AddFriendsPresenter(this);
    }
    private void addContact(){
        handler.sendEmptyMessage(200);
    }
}